package com.api.controller;

import cn.hutool.core.util.StrUtil;
import com.api.entity.*;
import com.api.log.Log;
import com.api.mapper.SysCodeMapper;
import com.api.utils.ParamsUtils;
import com.api.utils.Result;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.api.entity.table.MenusTableDef.MENUS;
import static com.api.entity.table.RolesTableDef.ROLES;
import static com.api.entity.table.SysCodeTableDef.SYS_CODE;
import static com.api.entity.table.UserRoleTableDef.USER_ROLE;

/**
 * @Author: yangsf
 * @CreateDate: 2024/10/31
 */

@Slf4j
@RestController
@RequestMapping("/sysCode")
public class SysCodeController extends BaseController{

    @Autowired
    SysCodeMapper codeMapper;

    public static String DATA_TYPE = "certificate_type";

    @Log("获取职称分类管理列表")
    @RequestMapping("/getList")
    public Result getList(@RequestParam Map<String,Object> params){
        QueryWrapper qw = new QueryWrapper();
        qw.select(SYS_CODE.ALL_COLUMNS)
        .where(SYS_CODE.IS_DEL.eq(0))
        .and(SYS_CODE.DATA_TYPE.eq(DATA_TYPE));
        if(params.containsKey("nameKeyword") && null != params.get("nameKeyword") && !StringUtils.isEmpty(params.get("nameKeyword"))){
            String nameKeyword = params.get("nameKeyword").toString();
            qw.and(SYS_CODE.CODE_NAME.like(nameKeyword));
        }
        if(params.containsKey("codePid") && null != params.get("codePid") && !StringUtils.isEmpty(params.get("codePid"))){
            String codePid = params.get("codePid").toString();
            qw.and(SYS_CODE.CODE_PID.eq(codePid));
        }
        Page page = ParamsUtils.toPage(params);
        Page<SysCode> pages =codeMapper.paginate(page,qw);
        return Result.ok(pages);
    }


    @Log("获取职称分类")
    @RequestMapping("/getCodeList")
    public Result getCodeList(String codeName, String codePid){

        QueryWrapper qw = QueryWrapper.create();
        qw.select(SYS_CODE.CODE_NAME,SYS_CODE.CODE_NUMBER);
        qw.where(SYS_CODE.IS_DEL.eq(0))
                .and(SYS_CODE.IS_SHOW.eq(1));
        if(StrUtil.isNotBlank(codeName)){
            qw.and(SYS_CODE.CODE_NAME.like(codeName));
        }
        if(StrUtil.isNotBlank(codePid)){
            qw.and(SYS_CODE.CODE_PID.eq(codePid));
        }
        qw.orderBy(SYS_CODE.CODE_SORT,true);
        List<SysCode> list = codeMapper.selectListWithRelationsByQuery(qw);
        return Result.ok(list);
    }

    @Log("获取职称分类树形结构")
    @RequestMapping("/getCodeTree")
    public Result getCodeTree(String codeName, String codePid){

        QueryWrapper qw = QueryWrapper.create();
        qw.where(SYS_CODE.CODE_PID.eq(0))
                .and(SYS_CODE.IS_DEL.eq(0))
                .and(SYS_CODE.IS_SHOW.eq(1))
                .orderBy(SYS_CODE.CODE_SORT,true);
        List<SysCode> list = codeMapper.selectListWithRelationsByQuery(qw);
        return Result.ok(list);
    }


    @Log("保存分类")
    @RequestMapping("/save")
    public Result save(@RequestBody SysCode code){
        boolean isAdd = null==code.getId()?true:false;
        if(isAdd){
            if(null == code.getCodePid()){
                code.setCodePid(0L);
            }

            SysCode last = SysCode.create().select(SYS_CODE.ALL_COLUMNS)
                    .where(SYS_CODE.CODE_PID.eq(code.getCodePid()))
                    .orderBy(SYS_CODE.CODE_NUMBER.desc())
                    .limit(1).one();
            if(null != last && null != last.getCodeNumber()){
                Integer codeNumber = last.getCodeNumber().intValue()+1;
                code.setCodeNumber(codeNumber.longValue());
            }
            else{
                code.setCodeNumber(1L);
            }
            code.setDataType("certificate_type");
            code.setDataDesc("工程师分类");
            code.setPidType("certificate_type");

            code.setIsDel(0);
            code.setIsSystem(0);
            code.setIsShow(1);
        }
        boolean b = code.saveOrUpdate();
        if(b) {
            return Result.ok();
        }
        return Result.err();
    }


    @Log("删除职称分类")
    @RequestMapping("/del")
    public Result del(Long id){
        if(null == id){
            return Result.err("未获取到分类ID");
        }
        SysCode code = SysCode.create()
                .where(SYS_CODE.ID.eq(id))
                .and(SYS_CODE.IS_DEL.eq(0))
                .one();
        if(null == code){
            return Result.err("未获取到分类信息");
        }
        boolean b = SysCode.create()
                .setId(id)
                .setIsDel(0)
                .updateById();
        if(b){
            return Result.ok();
        }
        return Result.err();
    }

}
